package com.zkzx.truck.controller;

import com.zkzx.common.exception.BaseException;
import com.zkzx.common.truck.command.TruckCommand;
import com.zkzx.common.truck.model.TruckListVO;
import com.zkzx.common.truck.model.TruckVO;
import com.zkzx.common.truck.param.TruckFilterParam;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.common.vo.StatusVO;
import com.zkzx.truck.service.TruckService;
import com.zkzx.util.json.JSONResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年05月10日 17时46分
 */
@RestController
@Validated
@Api( tags="车辆管理")
@RequestMapping(value = "/trucks")
public class TruckController extends BaseController {

    @Autowired
    private TruckService truckService;

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取企业列表", notes = "获取企业列表")
    public PageResult<TruckListVO> find(@ApiParam(value = "查询参数") @RequestParam("filterParam") String filterParamJson,
                                        @ApiParam(value = "分页查询参数")  @RequestParam("params") String paramsJson) {

        TruckFilterParam filterParam = (TruckFilterParam) JSONResultUtil.toObject(filterParamJson, TruckFilterParam.class);
        RequestFilterParams params = (RequestFilterParams) JSONResultUtil.toObject(paramsJson, RequestFilterParams.class);

        return truckService.find(filterParam,params);
    }

    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取车辆详细", notes = "获取车辆详细")
    public TruckVO getById(@PathVariable("id") Integer id) {
        return truckService.getById(id,super.getCompanyId());
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation(value = "新增车辆信息", notes = "新增车辆信息")
    public StatusVO add(@RequestBody @Valid TruckCommand command) throws BaseException {
        return truckService.add(command,super.getUserId());
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改车辆信息", notes = "修改车辆信息")
    public void update(@PathVariable("id") Integer id,@RequestBody @Valid TruckCommand command) {
        truckService.update(command,id,super.getUserId());
    }

    @PostMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "提交", notes = "提交")
    public StatusVO verification(@PathVariable("id") Integer id,@RequestParam("company_three_id") String companyThreeId) {

       return truckService.submit(id, companyThreeId,super.getCompanyId(),super.getUserId());
    }
}
